/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metro.jme.lottery.util;

import me.regexp.RE;

/**
 * This class provides a set of method to modify Strings.
 * @author pierre
 */
public final class StringUtil {

    private StringUtil(){
        
    }
    
    public static boolean isEmpty(String s) {
        return (s == null || "".equals(s));
    }

    public static String[] split(String s, String regex) {
        RE r = new RE(regex);
        return r.split(s);
    }
    
    public static int[] splitToInt(String s, String regex) throws Exception {
        int[] result;
        String[] splitted = split(s, regex);
        result = new int[splitted.length];
		for (int i = 0; i < splitted.length; i++) {
			try {
				result[i] = Integer.parseInt(splitted[i]);
			} catch(Exception e) {
				throw e;
			}
		}
		return result;
    }
    
    public static int[] explode(String s, char c) {
        int[] result = null;
        if (!StringUtil.isEmpty(s)) {
            RE re5 = new RE("(\\d{1,2})(" + c + "(\\d{1,2})){5,7}");
            if (re5.match(s)) {
                int n2 = re5.getParenCount();
                System.out.println("n2 = " + n2);
                int size = (n2) / 2;
                System.out.println("size = " + size);
                result = new int[size];
                int j = 0;
                for (int i = 1; i < n2; i += 2) {
                    result[j] = Integer.parseInt(re5.getParen(i));
                    j++;
                }
            }
        }
        return result;
    }
	
	public static String getParsable(String s) {
		String new_string = s;
		new_string = new_string.replace(',','.');
		String[] exploded = StringUtil.split(new_string, " ");
		new_string = "";
		for (int i = 0; i < exploded.length; i++) {
			new_string += exploded[i];
		}
		return new_string;
	}
}
